package tools

import (
	crypt2 "apibe/grpc/rome/v1/share/crypt"
	"regexp"
	"strconv"
	"strings"
)

// md5 Aa:大写|小写, +-:后加盐|前加盐, [0:16]:截取位数0-16) 示例：A+L[0:32]|salt
func (a *Argument) md5() {
	param := a.Function.Parameter
	value := a.Value
	split := strings.Split(param, "|")
	if len(split) == 0 {
		value = crypt2.MD5([]byte(value))
		return
	} else if len(split) == 1 {
		value = crypt2.MD5([]byte(value + param))
	} else if len(split) == 2 {
		if strings.Contains(split[0], "-") {
			value = split[1] + value
		} else {
			value = value + split[1]
		}
		value = crypt2.MD5([]byte(value))
		if strings.Contains(split[0], "A") {
			value = strings.ToUpper(value)
		}
		re := regexp.MustCompile("[0-9]+")
		cuts := re.FindAllString(split[0], 2)
		if len(cuts) == 2 {
			start, _ := strconv.Atoi(cuts[0])
			end, _ := strconv.Atoi(cuts[1])
			value = value[start:end]
		}
	}
	a.Value = value
}

// aesEncrypt 加密：
func (a *Argument) aesEncrypt() {
	secretKey := a.Function.Parameter
	value := a.Value
	encrypt := crypt2.AesEncrypt([]byte(value), secretKey)
	a.Value = string(encrypt)
}
